package com.gw.controller;

import com.gw.bean.UserInfoBean;
import com.gw.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
public class UserController {
    // 使用ioc、di进行依赖注入
    // 课下作业：spring的@Autowired注解和@Resources注解有什么异同点？
    @Autowired
    private IUserService userService;

    @RequestMapping("/login")
    public ModelAndView doLogin(UserInfoBean userInfo) {
        //返回对象
        ModelAndView modelAndView=new ModelAndView();
        // 调用service层进行登录业务逻辑处理
        List<UserInfoBean> lstUsers = userService.query4Login(userInfo);
        // 判断是否登录成功
        if (lstUsers != null && lstUsers.size() > 0) {
            // 登录成功
            // 返回登录成功的视图名

            modelAndView.setViewName("logined/main");
            modelAndView.addObject("loginedUser",lstUsers.get(0));
        } else {
            // 登录失败，返回登录页面
            modelAndView.setViewName("index");
        }
        return modelAndView;
    }
}
